package com.diandian.backend.client.service;

import com.diandian.backend.client.domain.ClientSetting;
import com.diandian.backend.client.dto.*;
import com.diandian.backend.client.mapper.ClientSettingMapper;
import com.diandian.backend.client.resp.ClientSettingGetByBelongResp;
import com.diandian.backend.client.resp.ClientSettingPageResp;
import com.diandian.base.boot.starter.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diandian.backend.client.resp.ClientSettingListResp;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClientSettingService {

    @Autowired
    private ClientSettingMapper clientSettingMapper;

    public PageResult page(ClientSettingPageParamDTO requestDTO){

        requestDTO.check();
        int pageSize = requestDTO.getPageSize();
        int pageNum = requestDTO.getPageNum();

        // 查询总数
        int totalCount = clientSettingMapper.getCount(requestDTO);

        // 查询数据集合
        List<ClientSetting> dataList = clientSettingMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<ClientSettingPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                ClientSettingPageResp item = new ClientSettingPageResp();
                item.setId(x.getId());
                item.setClientId(x.getClientId());
                item.setType(x.getType());
                item.setParamKey(x.getParamKey());
                item.setParamValue(x.getParamValue());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<ClientSettingListResp> getList(String clientId, String type){

        // 查询数据集合
        List<ClientSetting> dataList = clientSettingMapper.getList(clientId, type);

        // 组装数据
        List<ClientSettingListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                ClientSettingListResp item = new ClientSettingListResp();
                item.setId(x.getId());
                item.setClientId(x.getClientId());
                item.setType(x.getType());
                item.setParamKey(x.getParamKey());
                item.setParamValue(x.getParamValue());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return respList;
    }

    /**
     * 获取所有的belong属性值
     * @return
     */
    public List<String> getAllBelong(){
        List<String> belongList = clientSettingMapper.getAllBelong();
        return belongList == null ? new ArrayList<>() : belongList;
    }

    /**
     * 根据belong值获取其对应的clientId下的所有配置参数
     * @param belong
     * @return
     */
    public List<ClientSettingGetByBelongResp> getByBelong(String belong, String type){
        // 查询数据集合
        List<ClientSetting> dataList = clientSettingMapper.getByBelong(belong, type);

        // 组装数据
        List<ClientSettingGetByBelongResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                ClientSettingGetByBelongResp item = new ClientSettingGetByBelongResp();
                item.setClientId(x.getClientId());
                item.setType(x.getType());
                item.setParamKey(x.getParamKey());
                item.setParamValue(x.getParamValue());
                respList.add(item);
            });
        }

        return respList;
    }
}
